<!--
 * @Author: HeAo
 * @Date: 2021-10-14 16:46:08
 * @LastEditTime: 2021-10-14 17:21:08
 * @LastEditors: HeAo
 * @Description: 
 * @FilePath: \web2103-2105\web2103code\3-2-Vue\day06\4-vuex应用.html
 * 别乱动！
-->
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <script src="../js/vue.js"></script>
  <script src="../js/vuex.js"></script>
  <!-- 引入样式 -->
  <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
  <!-- 引入组件库 -->
  <script src="https://unpkg.com/element-ui/lib/index.js"></script>
  <script src="../js/axios.js"></script>
</head>

<body>
  <div id="app">
    <el-table :data="categories" style="width:100%">
      <el-table-column type="index" label="序号"></el-table-column>
      <el-table-column prop="id" label="编号"></el-table-column>
      <el-table-column prop="name" label="栏目名称"></el-table-column>
      <el-table-column prop="description" label="栏目描述"></el-table-column>
      <el-table-column prop="parentId" label="所属栏目"></el-table-column>
      <el-table-column label="操作">
        <template v-slot="scope">
          <el-button @click="deleteHandler(scope.row.id)" type="text">删除</el-button>
          <el-button @click="editHandler(scope.row)" type="text">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
  <script>
    let store = new Vuex.Store({
      state: {
        categories: []
      },
      mutations: {
        SET_CATEGORIES(state, payload) {
          state.categories = payload
        }
      },
      actions: {
        async findAllCategories({ commit }) {
          let res = await axios.get('http://47.94.46.113:8888/index/category/findAll')
          // console.log(res);
          commit('SET_CATEGORIES', res.data.data)
        }
      }
    })
    let { mapState, mapActions } = Vuex
    let vm = new Vue({
      el: '#app',
      data: {},
      computed: {
        ...mapState(['categories'])
      },
      methods: {
        ...mapActions(['findAllCategories']),
        // 删除按钮点击处理程序
        deleteHandler(id) {
          console.log(id);
        },
        // 修改按钮点击处理程序
        editHandler(row) {
          console.log(row);
        }
      },
      created() {
        this.findAllCategories()
      },
      store
    })
  </script>
</body>

</html>